﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;

namespace Math.Compiler.Expressions
{
    internal class Parameter : MathExpression
    {
        private MathExpression _expression;
        private Parameter _parameter;

        public Parameter(MathExpression expression, MathExpression args)
        {
            _expression = expression;
            _parameter = args as Parameter;
            if (_parameter != null)
            {
                Add(_parameter);
            }
        }

        public MathExpression MathExpression
        {
            get
            {
                return _expression;
            }
        }

        private Func<double, double, double> _eval;

        public override double Eval()
        {
            if (_parameter != null)
            {
                return _eval(_parameter, _expression);
            }
            return _expression;
        }

        public void Initialize(Func<double, double, double> eval, Function function)
        {
            _eval = eval;
            function.Add(_expression);
            if (_parameter != null)
            {
                _parameter.Initialize(eval, function);
            }
        }

        public Parameter Next
        {
            get
            {
                return _parameter;
            }
        }

#if DEBUG
        public override string ToString()
        {
            if (_parameter != null)
            {
                return _parameter.ToString();
            }
            return MathExpression.ToString();
        }
#endif
    }
}
